package cn.baiweigang.uu.api.future;

import cn.baiweigang.uu.BTContext;
import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.constants.BtcConfig;
import cn.baiweigang.uu.enums.BasisPriceTypeEnum;
import cn.baiweigang.uu.enums.ContractCodeEnum;
import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.BasisInfo;
import cn.baiweigang.uu.msg.BasisChangeEvent;
import cn.baiweigang.uu.msg.MsgPublisher;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.LogUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.huobi.future.wss.event.MarketBasisSubResponse;
import com.huobi.future.wss.handle.WssMarketHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
public class MarketBasisFutureWs extends BaseApi {
    private static String URL = "wss://" + BtcConfig.HB_HOST + "/ws_index";//交割合约站行情请求以及订阅地址
    private WssMarketHandle wssMarketHandle;
    private long preTs = 0;//前一次更新时间
    private BigDecimal preBasis = new BigDecimal("0");//前一次基差

    @Autowired
    private MsgPublisher msgPublisher;

    public boolean sub(ContractCodeEnum contractCode, PeriodEnum period, BasisPriceTypeEnum basisPriceType) {
        wssMarketHandle = new WssMarketHandle(URL);
        List<String> channels = Lists.newArrayList();
        channels.add("market." + contractCode.getCode() + ".basis." + period.getCode() + "." + basisPriceType.getCode());
        try {
            wssMarketHandle.sub(channels, response -> {
//                LogUtil.logDebug(response);
                long currentTimeMillis = DateUtil.getTs();
                BTContext.BasisRefreshTs = currentTimeMillis;
                MarketBasisSubResponse event = JSON.parseObject(response, MarketBasisSubResponse.class);
                MarketBasisSubResponse.TickBean tickBean = event.getTick();
                //基差大于100 或者超过10秒  发出消息
                if (currentTimeMillis - preTs > 10 * 1000 ||
                        tickBean.getBasis().subtract(preBasis).intValue() > 100) {
                    preTs = currentTimeMillis;
                    preBasis = event.getTick().getBasis();
                    BasisInfo basisInfo = new BasisInfo();
                    basisInfo.setId(tickBean.getId());
                    basisInfo.setBasis(tickBean.getBasis());
                    basisInfo.setBasisRate(tickBean.getBasisRate());
                    basisInfo.setContractCode(contractCode);
                    basisInfo.setPeriod(period);
                    basisInfo.setContractPrice(tickBean.getContractPrice());
                    basisInfo.setIndexPrice(tickBean.getIndexPrice());
                    basisInfo.setBasisPriceType(basisPriceType);
//                    basisInfo.setDateTime(DateUtil.formatDateYMDHMS(new Date(basisInfo.getId() * 1000)));
                    BasisChangeEvent basisChangeEvent = new BasisChangeEvent("MarketBasisFutureWs",
                            contractCode, period, basisInfo);
                    LogUtil.logInfo("发出消息");
                    LogUtil.logInfo(basisChangeEvent);
                    msgPublisher.publish(basisChangeEvent);
                }
            });
            return true;
        } catch (Throwable e) {
            LogUtil.logError(e);
        }
        return false;

    }

    public void setWsNull() {
        if (wssMarketHandle != null) wssMarketHandle.close();
        wssMarketHandle = null;
    }
}
